
const ListController = {};
const dbQuery = require('../model/model.js')
const { dateFormat, getNowDate } = require('../util/tool')

//分类列表
ListController.list = (req, res) => {
    let userInfo = JSON.parse(req.session.userInfo || '{}');
    res.render('cate-list.html', { userInfo })
}

// 获取分类数据的api接口
ListController.cate = (req, res) => {
    let sql = 'select * from articletable';
    dbQuery(sql).then(result => {
        result.forEach(item => {
            item['Add_time'] = dateFormat(item['Add_time'])
        })
        res.json(result)
    })
}

//删除分类
ListController.delete = (req, res) => {
    let { id } = req.body;
    console.log({ id })
    let sql = `delete from articletable where id = ${id}`;

    dbQuery(sql).then(result => {
        console.log(result);
        if (result.affectedRows) {
            res.json({
                code: 20000,
                message: "成功"
            })
        } else {
            res.json({ code: -1, message: "失败" })
        }
    })

}

//添加分类
ListController.article = (req, res) => {
    let userInfo = JSON.parse(req.session.userInfo || "{}");
    res.render("article.html", { userInfo })
}

//添加分类数据入库
ListController.axAddCate = (req, res) => {
    let { name, Show_timeornot } = req.body
    console.log(req.body)
    //判断是否存在分类
    let sql = `select * from articletable where name ='${name}'`
    dbQuery(sql).then(result => {
        if (result.length) {
            res.json({
                code: -1,
                message: '已存在该分类目录'
            })
        } else {
            let Add_time = getNowDate()
            let sql = `insert into articletable(name,Show_timeornot,Add_time)
               values('${name}','${Show_timeornot}','${Add_time}')`
               dbQuery(sql).then(result => {
                if (result.affectedRows) {
                    res.json({
                        code: 20000,
                        message: '添加成功'
                    })
                    
                // } else {
                //     res.json({
                //         code: -1,
                //         message: '已存在该目录'
                //     })
                }
            })
        }
    })
}

//展示分类页面
ListController.delete = async(req,res)=>{
    let{id} =req.body;
    let sql = `delete from articletable where id = ${id}`;
    try{
        let result = await dbQuery(sql)
        res.json({
            code:20000,
            message:'成功'
        })
        
    }catch(err){
        res.json({
            code:-1,
            message:'faial'
        })
    }
}
 
//输出一个编辑分类 页面
ListController.editCate = (req, res) => {
    let userInfo = JSON.parse(req.session.userInfo || '{}');
    res.render('editCate.html', { userInfo })
}

//获取当前指定的分类
ListController.getCateDetail = async(req,res)=>{
    let{id} = req.query;
    let sql = `select * from articletable where id = ${id}`
    try{
        let data = await dbQuery(sql)
        res.json(data[0] || {} )
    }catch(err){
        res.json({})
    }
}

//更新分类
ListController.updateCate = async(req,res)=>{
   const {id,name,Show_timeornot,content} = req.body;
  
    let sql = `select id from articletable where name = '${name}' and id !='${id}'`;
    let result = await dbQuery(sql)
    // console.log(result)
    //判断数据库是否有同名分类
   if(result.length){
        res.json({
            message:'分类名已存在',
            code:-1
        })
        return;
    }
    //没有同名分类

    let sql2 = `update articletable set name = "${name}",Show_timeornot = '${Show_timeornot}',content = '${content}' where id = '${id}'`;
    let result2 = await dbQuery(sql2)
    if(result2.affectedRows){
        res.json({
            message:'更新成功',
            code:20000
        })
    }else{
        res.json({
            message:'编辑失败',
            code:-1
        })
    }
    

}






module.exports = ListController;
